<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>AI设置</title>
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
    <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
</head>
<body class="pear-container">

<div class="layui-card">
    <div class="layui-card-body">

        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">通用设置</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <form class="layui-form" lay-filter="setting" onsubmit="return false">
                        <div class="layui-form-item">
                            <label class="layui-form-label">开启推广</label>
                            <div class="layui-input-inline">
                                <input type="checkbox" name="enable_invitation" lay-skin="switch">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">必须登录</label>
                            <div class="layui-input-inline">
                                <input type="checkbox" name="need_login" lay-skin="switch">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">其它</label>
                            <div class="layui-input-inline">
                                <input type="text" name="footer_txt" placeholder="其它展示在页面底部的内容" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="pear-btn pear-btn-primary" lay-submit="" lay-filter="setting">保 存</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

        </div>

    </div>
</div>


<script src="/app/admin/component/layui/layui.js"></script>
<script src="/app/admin/component/pear/pear.js"></script>
<script src="/app/admin/admin/js/permission.js"></script>
<script src="/app/admin/admin/js/common.js"></script>


<style>
    .layui-form-label {
        width: 100px;
    }
</style>

<script>

    function getSetting() {
        let form = layui.form;
        let $ = layui.jquery;

        // 基础配置
        $.ajax({
            url: "/app/ai_invitation/admin/setting/select",
            success: function (res) {
                if (res.code) {
                    return layui.popup.failure(res.msg);
                }
                for (let name in res.data) {
                    if (["category", "model", "plan", "sensitive-word"].includes(name)) {
                        let obj = {};
                        obj[name] = res.data[name];
                        form.val(name, obj);
                    } else {
                        form.val(name, res.data[name]);
                    }
                    if (!getSetting.inited) {
                        form.on("submit(" + name + ")", function (data) {
                            $.ajax({
                                url: "/app/ai_invitation/admin/" + name + "/update",
                                dataType: "json",
                                type: "POST",
                                data: data.field,
                                success: function (res) {
                                    if (res.code) {
                                        return layui.popup.failure(res.msg);
                                    }
                                    getSetting();
                                    return layui.popup.success("操作成功");
                                }
                            });
                            return false;
                        });
                    }
                }
                getSetting.inited = true;
            }
        });
    }

    layui.use(["form", "popup"], getSetting);
</script>
</body>
</html>
